package com.example.lib_base.base.activity


import android.os.Bundle

import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.base.util.event.BaseEvent
import com.example.lib_base.base.util.event.EventBusUtil
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
        ARouter.getInstance().inject(this)
        initContentView()
        initialize(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        if (isRegisterEventBus()){
            EventBusUtil.register(this);
        }
    }

    override fun onStop() {
        super.onStop()
        if (isRegisterEventBus()){
            EventBusUtil.unregister(this);
        }
    }

    /**
     * 初始化视图
     */
    protected open fun initContentView() {
        setContentView(getLayoutId())
    }
    /**
     * 设置视图资源id
     */
    @LayoutRes
    protected abstract fun getLayoutId(): Int
    /**
     *  初始化操作
     */
    protected abstract fun initialize(savedInstanceState: Bundle?)

    /**
     * 是否注册EventBus 默认不注册
     */
    protected open fun isRegisterEventBus(): Boolean {
        return false
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventBusCome(event:BaseEvent) {
        event?.apply {
            receiveEvent(this)
        }
    }

    /**
     * 接受到的EventBus消息处理
     */
    protected open fun receiveEvent(event: BaseEvent) {

    }

}